package com.fast.user.config;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.jedis.JedisConnectionFactory;
import org.springframework.session.data.redis.config.annotation.web.http.EnableRedisHttpSession;
import org.springframework.session.web.http.HeaderHttpSessionStrategy;
import org.springframework.session.web.http.HttpSessionStrategy;

import com.fast.user.common.constants.Parameters;

/**
 * @author wangzun
 * TODO redis -session config
 * */
@Configuration
@EnableRedisHttpSession
public class HttpSessionConfig {
	
	@Autowired
	private Parameters parameters;
	
	/**
	 * 重写SpringSession 选择策略
	 * @return 采用HeaderHttpSession策略
	 * */
	@Bean
	public HttpSessionStrategy httpSessionStrategy() {
		return new HeaderHttpSessionStrategy();
	}
	
	/**
	 * 配置redis
	 * */
	@Bean
	public JedisConnectionFactory connectionFactory() {
		JedisConnectionFactory connectionFactory = new JedisConnectionFactory();
		String redisHost = parameters.getRedisNode().split(":")[0];
		int redisPort = Integer.valueOf(parameters.getRedisNode().split(":")[1]);
		
		connectionFactory.setTimeout(2000);
		connectionFactory.setHostName(redisHost);
		connectionFactory.setPort(redisPort);
		connectionFactory.setPassword(parameters.getRedisAuth());
		
		return connectionFactory;
	}

}
